home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 4 / The Pier Shareware #4 (The Pier Exchange) (1994).ISO / 038 / prochook.exe / HOOKTOOL.C < prev    next >
C/C++ Source or Header  |  1994-01-01  |  8KB  |  248 lines

  1. /*
  2.   HOOKTOOL.C - Copyright (c) 1993 James M. Finnegan, All Rights Reserved
  3. */
  4. #include <windows.h>
  5. #include <toolhelp.h>
  6. #include <dos.h>
  7. #include "hooktool.h"
  8. #include "prochook.h"
  9. #include "goodies.h"
  10.   
  11.  
  12. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine,
  13.            int nCmdShow)
  14. {
  15.     static char szAppName[]="HookTool";
  16.     HWND        hwnd;
  17.     MSG         msg;
  18.     WNDCLASS    wndclass;
  19.  
  20.  
  21.     if(!hPrevInstance)
  22.     {
  23.         wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  24.         wndclass.lpfnWndProc   = WndProc;
  25.         wndclass.cbClsExtra    = 0;
  26.         wndclass.cbWndExtra    = DLGWINDOWEXTRA;
  27.         wndclass.hInstance     = hInstance;
  28.         wndclass.hIcon         = LoadIcon(hInstance,szAppName);
  29.         wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  30.         wndclass.hbrBackground = COLOR_WINDOW + 1;
  31.         wndclass.lpszMenuName  = NULL;
  32.         wndclass.lpszClassName = szAppName;
  33.  
  34.         if(!RegisterClass(&wndclass))
  35.             return -1;
  36.     }
  37.  
  38.     if((hwnd=CreateDialog(hInstance,szAppName,0,NULL)) == NULL)
  39.         return -1;
  40.  
  41.     ShowWindow(hwnd,nCmdShow);
  42.  
  43.     while(GetMessage(&msg,NULL,0,0))
  44.     {
  45.         if((!IsWindow(hwnd)) ||
  46.            (!IsDialogMessage(hwnd,&msg)))
  47.         {
  48.             TranslateMessage(&msg);
  49.             DispatchMessage(&msg);
  50.         }
  51.     }
  52.  
  53.     return msg.wParam;
  54. }
  55.  
  56.  
  57. long WINAPI WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
  58. {
  59.     switch(message)
  60.     {
  61.         case WM_CREATE:
  62.             //Center the window on the screen
  63.             CenterWindow(hWnd);
  64.  
  65.              // Post messages to set focus and refresh the list box
  66.             PostMessage(hWnd,WM_SFOCUS,0,0L);
  67.             PostMessage(hWnd,WM_COMMAND,IDB_REFRESH,0L);
  68.             break;
  69.         
  70.         case WM_SFOCUS:    
  71.             SetFocus(GetDlgItem(hWnd,IDB_REFRESH));
  72.             break;
  73.         
  74.         // Processing for the listbox buttons...
  75.         case WM_COMMAND:
  76.             switch(wParam)
  77.             {
  78.                 // Reset the listboxes and walk the master list
  79.                 case IDB_REFRESH:
  80.                     SendDlgItemMessage(hWnd,IDL_LIST1,LB_RESETCONTENT,0,0L);
  81.                     SendDlgItemMessage(hWnd,IDL_LIST2,LB_RESETCONTENT,0,0L);
  82.                     EnableWindow(GetDlgItem(hWnd,IDB_DELETE),FALSE);
  83.                     WalkMaster(hWnd);
  84.                     break;
  85.                                 
  86.                 // Delete the currently selected child record
  87.                 case IDB_DELETE:
  88.                     if(MessageBox(hWnd,"Delete. Are you sure?","HookTool",
  89.                           MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2) == IDYES)
  90.                     {
  91.                         DeleteChild(hWnd);
  92.                         PostMessage(hWnd,WM_COMMAND,IDB_REFRESH,0L);
  93.                     }                    
  94.                     break;
  95.                     
  96.                 case IDB_EXIT:
  97.                     PostMessage(hWnd,WM_CLOSE,0,0L);
  98.                     break;
  99.                     
  100.                 case IDL_LIST1:
  101.                     switch(HIWORD(lParam))
  102.                     {
  103.                         // Reset and populate the second listobx if a 
  104.                         // new master record is selected in the first
  105.                         // listbox
  106.                         case LBN_SELCHANGE:
  107.                             SendDlgItemMessage(hWnd,IDL_LIST2,LB_RESETCONTENT,0,0L);
  108.                             EnableWindow(GetDlgItem(hWnd, IDB_DELETE),FALSE);
  109.                             WalkChild(hWnd);
  110.                             break;
  111.                     }
  112.                     break;
  113.  
  114.                 case IDL_LIST2:
  115.                    // Set the status of the delete button based on whether
  116.                    // there is a selection in the second listbox
  117.                    if(SendDlgItemMessage(hWnd,IDL_LIST1,LB_GETCURSEL,0,0L)
  118.                                                                   != LB_ERR)
  119.                    {    
  120.                        EnableWindow(GetDlgItem(hWnd,IDB_DELETE),TRUE);
  121.                    }
  122.                    else
  123.                    { 
  124.                        EnableWindow(GetDlgItem(hWnd,IDB_DELETE),FALSE);
  125.                    }
  126.                    break;    
  127.             }
  128.             break;
  129.             
  130.         case WM_DESTROY:
  131.             PostQuitMessage(0);
  132.             break;
  133.  
  134.         default:
  135.             return DefWindowProc(hWnd, message, wParam, lParam);
  136.             break;
  137.     }
  138.     return 0L;
  139. }
  140.  
  141.  
  142. VOID WalkMaster(HWND hWnd)
  143. {
  144.     char         szText[50];
  145.     GLOBALENTRY  ge;
  146.     HOOKMASTER   HookMast;
  147.     NPHOOKMASTER npHookMast;
  148.     WORD         wIndex;
  149.    
  150.    
  151.     // Get the first master record
  152.     if((npHookMast=GetFirstHMaster(&HookMast)) == NULL)
  153.         return;
  154.   
  155.     for(;;)
  156.     {
  157.         // Get the owner of the hook
  158.         ge.dwSize=sizeof(GLOBALENTRY);
  159.         GlobalEntryHandle(&ge,FP_SEG(HookMast.lpfnHookedFunction));
  160.  
  161.         wsprintf(szText,"%s.%s",(LPSTR)GetModuleName(ge.hOwner),
  162.                                        GetNameFromOrd(ge.hOwner,
  163.                                        GetOrdFromAddr(ge.hOwner,HookMast.lpfnHookedFunction)));
  164.  
  165.         // Print the string showing the owner and export in the listbox
  166.         if((wIndex=(WORD)SendDlgItemMessage(hWnd,IDL_LIST1,LB_ADDSTRING,0,
  167.                                              (LONG)(LPSTR)szText)) != LB_ERR)
  168.         {
  169.             // Associate the master pointer with the string
  170.             SendDlgItemMessage(hWnd,IDL_LIST1,LB_SETITEMDATA,wIndex,
  171.                                                     MAKELONG(npHookMast,0));
  172.         }
  173.        
  174.         // Get the next master record and loop
  175.         if((npHookMast=GetNextHMaster(&HookMast)) == NULL)
  176.             break;
  177.     }
  178. }     
  179.  
  180.  
  181. VOID WalkChild(HWND hWnd)
  182. {
  183.     char         szText[50];
  184.     GLOBALENTRY  ge;
  185.     NPHOOKMASTER npHookMast;
  186.     HOOKCHILD    HookChild;
  187.     NPHOOKCHILD  npHookChild;
  188.     WORD         wIndex;
  189.                  
  190.                  
  191.     // Get the current master selection
  192.     if((wIndex=(WORD)SendDlgItemMessage(hWnd,IDL_LIST1,LB_GETCURSEL,0,0L)) == LB_ERR)
  193.         return;
  194.         
  195.     // Get the pointer to the master
  196.     npHookMast=(NPHOOKMASTER)SendDlgItemMessage(hWnd,IDL_LIST1,
  197.                                                    LB_GETITEMDATA,wIndex,0L);
  198.    
  199.     // Get the first child record of the current master
  200.     if((npHookChild=GetFirstHChild((LPHOOKMASTER)MK_FP(0,npHookMast),&HookChild)) == NULL)
  201.     {
  202.         // If the child record is not valid, print "Invalid"
  203.         SendDlgItemMessage(hWnd,IDL_LIST2,LB_ADDSTRING,0,(LONG)(LPSTR)"Invalid");
  204.         return;
  205.     }    
  206.  
  207.     for(;;)
  208.     {
  209.         // Get the owner of the hook
  210.         ge.dwSize=sizeof(GLOBALENTRY);
  211.         GlobalEntryHandle(&ge,FP_SEG(HookChild.lpfnNewFunc));
  212.     
  213.         wsprintf(szText,"%s",(LPSTR)GetModuleName(ge.hOwner));
  214.  
  215.         // Print the owner in the listbox
  216.         if((wIndex=(WORD)SendDlgItemMessage(hWnd,IDL_LIST2,LB_ADDSTRING,0,
  217.                                              (LONG)(LPSTR)szText)) != LB_ERR)
  218.         {
  219.             // Associate the child pointer with the string
  220.             SendDlgItemMessage(hWnd,IDL_LIST2,LB_SETITEMDATA,wIndex,
  221.                                                    MAKELONG(npHookChild,0));
  222.         }
  223.  
  224.         // Get the next child record and loop
  225.         if((npHookChild=GetNextHChild(&HookChild)) == NULL)
  226.             break;
  227.     }
  228. }
  229.  
  230.  
  231. VOID DeleteChild(HWND hWnd)
  232. {   
  233.     WORD        wIndex;
  234.     NPHOOKCHILD npHookChild;
  235.                  
  236.                  
  237.     // Get the current child selection
  238.     if((wIndex=(WORD)SendDlgItemMessage(hWnd,IDL_LIST2,LB_GETCURSEL,0,0L))
  239.                                                                   !=LB_ERR)
  240.     {
  241.         // Get the pointer to the child record
  242.         npHookChild=(NPHOOKCHILD)SendDlgItemMessage(hWnd,IDL_LIST2,
  243.                                                    LB_GETITEMDATA,wIndex,0L);
  244.         // Delete it!
  245.         SetProcRelease(npHookChild);
  246.     }
  247. }
  248.